﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XGameLibrary;
using XGameLibrary.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using TomShane.Neoforce.Controls;

namespace XGame.GameScreens
{
    public class StartMenuScreen : XGameScreenBase
    {
        #region Field region

        PictureBox backgroundImage;
        PictureBox arrowImage;
        LinkLabel startGame;
        LinkLabel loadGame;
        LinkLabel returnTitle;
        LinkLabel exitGame;
        float maxItemWidth = 0f;

        #endregion

        #region Property Region
        #endregion

        #region Constructor Region

        public StartMenuScreen(Manager Manager, GameScreenManager manager)
            : base(Manager, manager)
        {
        }

        #endregion

        #region XNA Method Region

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            ContentManager Content = Game.Content;

            backgroundImage = new PictureBox(manager,
                Content.Load<Texture2D>(@"Image\TitleScreen"),
                new Rectangle(
                0,
                0,
                InputHandler.ScreenWidth,
                InputHandler.ScreenHeight));
            ControlManager.Add(backgroundImage);

            Texture2D arrowTexture = Content.Load<Texture2D>(@"GUI\rightarrowUp");

            arrowImage = new PictureBox(manager,
                arrowTexture,
                new Rectangle(
                    0,
                    0,
                    arrowTexture.Width,
                    arrowTexture.Height));
            ControlManager.Add(arrowImage);

            startGame = new LinkLabel(manager);
            startGame.Text = "The story begins";
            startGame.Size = startGame.SpriteFont.MeasureString(startGame.Text);
            startGame.Selected += new System.EventHandler(menuItem_Selected);

            ControlManager.Add(startGame);

            loadGame = new LinkLabel(manager);
            loadGame.Text = "The story continues";
            loadGame.Size = loadGame.SpriteFont.MeasureString(loadGame.Text);
            loadGame.Selected += menuItem_Selected;

            ControlManager.Add(loadGame);

            returnTitle = new LinkLabel(manager);
            returnTitle.Text = "Return title";
            returnTitle.Size = returnTitle.SpriteFont.MeasureString(returnTitle.Text);
            returnTitle.Selected += menuItem_Selected;

            ControlManager.Add(returnTitle);

            exitGame = new LinkLabel(manager);
            exitGame.Text = "The story ends";
            exitGame.Size = exitGame.SpriteFont.MeasureString(exitGame.Text);
            exitGame.Selected += menuItem_Selected;

            ControlManager.Add(exitGame);

            //ControlManager.NextControl();

            ControlManager.FocusChanged += new System.EventHandler(ControlManager_FocusChanged);

            Vector2 position = new Vector2(300, 250);

            foreach (XGameLibrary.Controls.Control c in ControlManager)
            {
                if (c is LinkLabel)
                {
                    if (c.Size.X > maxItemWidth)
                        maxItemWidth = c.Size.X;

                    c.Position = position;
                    position.Y += c.Size.Y + 5f;
                }
            }

            ControlManager_FocusChanged(startGame, null);
        }

        void ControlManager_FocusChanged(object sender, System.EventArgs e)
        {
            XGameLibrary.Controls.Control control = sender as XGameLibrary.Controls.Control;
            Vector2 position = new Vector2(control.Position.X - 50f, control.Position.Y);
            arrowImage.SetPosition(position);
        }

        private void menuItem_Selected(object sender, System.EventArgs e)
        {
            if (sender == startGame)
                Transition(GameScreenManager.ChangeType.Push, GameRef.GamePlayScreen);

            //if (sender == loadGame)
            //    Transition(GameScreenManager.ChangeType.Push, GameRef.LoadGameScreen);

            if (sender == returnTitle)
                Pop();

            if (sender == exitGame)
                GameRef.Exit();
        }

        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();

            base.Draw(gameTime);

            ControlManager.Draw(GameRef.SpriteBatch);

            GameRef.SpriteBatch.End();
        }

        #endregion

        #region Game State Method Region
        #endregion

    }
}
